# -*- coding: utf-8 -*-
# File : patch_regular.py
# Author: taoyahui
# Date : 2021/7/16
from utils import mysql_util, common_util
import datetime
from utils.log_util import logger


def patch_auto_regular():
    """
    补丁自动转正
    :return:
    """
    logger.info("*" * 10 + " patch_auto_regular -> 开始执行补丁自动转正" + "*" * 10)
    query_sql = "SELECT id, prepare_regular_time, auto_official_hour from patch"
    patch_list = mysql_util.exec_sql_new(query_sql)
    for patch in patch_list:
        id = patch[0]
        prepare_regular_time = patch[1]
        auto_official_hour = patch[2]
        if prepare_regular_time is None:
            current_time = common_util.time_str_format()
            update_sql = f"UPDATE patch set prepare_regular_time = '{current_time}' where id = {id}"
            mysql_util.exec_sql_new(update_sql)
            prepare_regular_time = datetime.datetime.now()
        if auto_official_hour is None:
            auto_official_hour = 72
        if datetime.datetime.now() > prepare_regular_time + datetime.timedelta(hours=auto_official_hour):
            regular_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            regular_time_update_sql = f"UPDATE patch set regular_time = '{regular_time}' where id = {id}"
            mysql_util.exec_sql_new(regular_time_update_sql)
    logger.info("*" * 5 + " patch_auto_regular -> 完成执行补丁自动转正" + "*" * 5)


if __name__ == '__main__':
    patch_auto_regular()
